﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;

//http://atlas.resources.ca.gov/ArcGIS/SDK/REST/export.html

namespace WMSTest.OnlineMapRequests
{
    class ArcOnlineRESTMapRequest : MapRequest
    {
        #region Inner Classes
       
        public enum Format
        {
            HTML, JSON, IMAGE, KMZ
        }

        public enum ImageFormat
        {
            PNG, PNG8, PNG24, JPG, PDF, BMP, GIF, SVG, PNG32
        }

        #endregion

        #region Class members
        
        public Format format { get; set; }
        public Extent bbox { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public Int16 dpi { get; set; }
        public string imageSR { get; set; }
        public string bboxSR { get; set; }
        public ImageFormat imageFormat { get; set; }
        public Boolean transparent { get; set; }

        #endregion
        
        #region Constructors


        public ArcOnlineRESTMapRequest(string endpointUrl) 
            : base(endpointUrl, RequestMethod.POST)
        {
            this.format = Format.IMAGE;
            this.imageFormat = ImageFormat.PNG;
            this.transparent = false;
          
        }

        public ArcOnlineRESTMapRequest(string endpointUrl, Format format, int width, int height, Int16 dpi, 
            string imageSR, string bboxSR, ImageFormat imageFormat, Boolean transparent) : base(endpointUrl, RequestMethod.POST)
        {
            this.format = format;
            this.Width = width;
            this.Height = height;
            this.dpi = dpi;
            this.imageSR = imageSR;
            this.bboxSR = bboxSR;
            this.imageFormat = imageFormat;
            this.transparent = transparent;
        }
       
        #endregion

        #region Public Methods

        public override HttpWebResponse PerformExportMapRequest()
        {
            //setup the paramtable
            httpRequest.paramTable = new Hashtable();

            httpRequest.paramTable.Add("bbox", bbox.ToString());
            httpRequest.paramTable.Add("f", Enum.GetName(typeof(Format), format));

            httpRequest.paramTable.Add("size", Width + "," + Height);
            httpRequest.paramTable.Add("dpi", dpi);
            httpRequest.paramTable.Add("imageSR", imageSR);
            httpRequest.paramTable.Add("bboxSR", bboxSR);
            httpRequest.paramTable.Add("format", this.ImageFormatString);
            httpRequest.paramTable.Add("transparent", transparent);

            //then call parent method
            return  httpRequest.IssueRequest();
        }

        public override void SetSize(int width, int height)
        {
            Width = width;
            Height = height;
        }

        public override void SetBbox(double xmin, double ymin, double xmax, double ymax)
        {
            if (bbox == null)
            {
                bbox = new Extent(xmin, ymin, xmax, ymax);
            }
            else
            {
                bbox.xmin = xmin;
                bbox.ymin = ymin;
                bbox.xmax = xmax;
                bbox.ymax = ymax;
            }
        }

        #endregion

        #region Properties

        public override string ImageFormatString
        {
            get
            {
                return Enum.GetName(typeof(ImageFormat), imageFormat);
            }

        }

        #endregion

    }
}
